package com.lanqiao.gxnushop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanqiao.gxnushop.entity.Buyers;
import com.lanqiao.gxnushop.service.BuyersService;
import com.lanqiao.gxnushop.util.UUIdUtil;
import com.lanqiao.gxnushop.valueObject.Page;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.io.File;
import java.util.List;


@Log4j
@Tag(name = "BuyersController",description="买家管理")
@RestController
public class BuyersController {
    @Autowired
    private BuyersService buyersService;

    //似乎用不到
    @PostMapping("/buyers")
    @Operation(summary="增加买家信息")
    @Parameter(name = "buyers",description = "买家信息:buyerId(不为null),name,email,yhtx,phone,zcsj(已自动加入)")
    public boolean addBuyers(Buyers buyers) {
        return buyersService.add(buyers);
    }

    @GetMapping("/buyers")
    @Operation(summary = "查询所有买家")
    public PageInfo<Buyers> findAllBuyers(Page pageable) {
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Buyers> list = buyersService.findAll();
        return new PageInfo<>(list);
    }

    @GetMapping("/buyers/selectName")
    @Operation(summary = "根据买家姓名模糊查询买家信息")
    @Parameter(name = "name", description = "买家姓名:name(不为null)")
    public PageInfo<Buyers> findByName(@RequestParam String name, Page pageable) {
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Buyers> list = buyersService.findByName(name);
        return new PageInfo<>(list);
    }

    @DeleteMapping("/buyers/{buyerId}")
    @Operation(summary="删除买家信息")
    @Parameter(name = "buyerId",description = "买家id:buyerId(不为null)")
    public boolean deleteBuyers(@PathVariable String buyerId) {
        return buyersService.delete(buyerId);
    }

    @GetMapping("/buyers/{buyerId}")
    @Operation(summary="根据买家id查询买家信息")
    @Parameter(name = "buyerId",description = "买家id:buyerId(不为null)")
    public Buyers findById(@PathVariable String buyerId) {
        return buyersService.findById(buyerId);
    }

    @PutMapping("/buyers")
    @Operation(summary="根据买家buyerId(不为null)更新买家信息")
    @Parameter(name = "buyers",description = "买家信息:name,email,yhtx,phone")
    public boolean updateBuyers(Buyers buyers) {
        return buyersService.update(buyers);
    }

    @Operation(summary = "上传买家头像")
    @Parameter(name="pic",description = "用户头像图片" ,content = @Content(mediaType = "multipart/form-data")  )
    @Parameter(name="buyerId",description = "用户编号")
    @PostMapping("/buyersCtrl/upload")
    public String upload(@RequestPart("pic") MultipartFile pic, @RequestParam("buyerId") String buyerId)  {
        String fileName = null;
        log.debug("--------------------------" + buyerId);
        try {
            if (!pic.isEmpty() && ImageIO.read(pic.getInputStream()) != null) {
                //获取文件名
                fileName = pic.getOriginalFilename();
                //通过UUID 设置新的文件名
                fileName = UUIdUtil.getUserId() + fileName.substring(fileName.lastIndexOf("."));
                //创建目标文件
                File file = new File(fileName);
                //将照片保存到目标文件
                pic.transferTo(file);

                //将图片名保存到数据库
                Buyers buyers = new Buyers();
                buyers.setBuyerId(buyerId);
                buyers.setYhtx(fileName);
                log.debug("--------------------------" + buyers);
                buyersService.updateYhtx(buyers);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }
}
